import { defineMock } from 'vite-plugin-mock-dev-server'
const fs = require('fs')
const path = require('path')
const root = process.cwd()

// mock接口列表，可完全离线测试
const apiList = [
  '/api/user/login', // 登录
  '/api/user/current_user', // 获取登录信息
  '/api/getPermissions', // 权限
  '/api/table/meta', // 表格字段
  '/api/table/search', // 表格查询
  '/api/form/get_values' // 通用下拉框
]
const apis = apiList.map((url: any) => ({
  url,
  delay: 300,
  body: () =>
    JSON.parse(
      fs.readFileSync(path.resolve(root, `./mock/json/${url.split('/').reverse()[0]}.json`), 'utf8')
    )
}))

export default defineMock([
  {
    url: '/api/basic/get_configs',
    delay: 1000,
    body: () => import('./json/get_configs.json').then((res) => res.default)
  },
  ...apis
])
